import datetime
import os
import random
import time

import requests
import base64
import json
import re

'''
cron: 10 0 20 * * *
new Env("查看im是否有签到")
'''

from notify import send
from Crypto.Cipher import AES

# 青龙面板加入环境变量im_token

def getSignInfo(url, userId, token):
    """
    获取签到信息
    :return:
    """

    headers = {
        'X-Requested-With': 'XMLHttpRequest',
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_2_1 like Mac OS X)',
        'Client-User-Agent': 'iOS;15.0.1;Apple;iPhone 12 ProMax',
        'Origin': url,
        'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
    }

    jsonData = {
        "userId": userId,
        "token": token
    }

    response = requests.post(url + '/api/task/signInfo', cookies={},
                             headers=headers, json=jsonData)
    s = ""
    if (response.json().get('status') == 0):
        if response.json().get('data').get('signFlag'):
            s = "已签到：连续签到" + str(response.json().get('data').get('continuous')) + "天，当前积分为" + str(response.json().get
            ('data').get('score'))
        else:
            s = "未签到：" + str(response.json().get('data'))
    else:
        s = "获取签到信息失败," + response.text
    return s

if __name__ == '__main__':
    im_tokens = os.getenv("im_token")
    url = os.getenv("im_url")
    if not im_tokens:
        print('im_tokens is null')
        exit()
    im_token_list = im_tokens.split('&')
    s = "-----------------总共" + \
        str(int(len(im_token_list))) + \
        "个用户-----------------" + '\n'
    userCount = 0
    if len(im_token_list) > 0:
        for im_token in im_token_list:
            userCount += 1
            userId, token = im_token.split(',')
            s += "第" + str(userCount) + "个用户" + userId
            try:
                r = getSignInfo(url, userId, token)
                s += "," + r + '\n              \n'
            except Exception as e:
                s += "异常信息" + e
    print(s)
    send("im任务", s)
